package com.ch.base.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CaptchaServlet extends HttpServlet {
	private int height=0;
	private int width=0;
		
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
	 height = Integer.parseInt(getServletConfig().getInitParameter("height"));
     width = Integer.parseInt(getServletConfig().getInitParameter("width"));
  }

 
 protected void doGet(HttpServletRequest req, HttpServletResponse response) throws IOException, ServletException {
	  //Expire response
	  response.setHeader("Cache-Control", "no-cache");
	  response.setDateHeader("Expires", 0);
	  response.setHeader("Pragma", "no-cache");
	  response.setDateHeader("Max-Age", 0);
		
	  BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
	  Graphics2D graphics2D = image.createGraphics();
	  // ----------  增加下面的代码使得背景透明  -----------------
	  //image = graphics2D.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
	  //graphics2D = image.createGraphics();
	  // ----------  背景透明代码结束  -----------------
	  Random r = new Random();
	  String token = Long.toString(Math.abs(r.nextLong()), 36);
	  String ch = token.substring(0,6);
	  Color c = new Color(0.6662f, 0.4569f, 0.3232f);
	  //gradientPaint 文字的顏色(漸層)
	  GradientPaint gradientPaint = new GradientPaint(30, 30, c, 15, 25, Color.white, true);
	  graphics2D.setPaint(gradientPaint);
	  Font font = new Font("Verdana", Font.CENTER_BASELINE , 26);
	  graphics2D.setFont(font);
	  graphics2D.drawString(ch, 2, 20);
	  graphics2D.dispose();
	  
	  HttpSession session = req.getSession(true);
	  session.setAttribute("captchaKey",ch);

	  OutputStream outputStream = response.getOutputStream();
	  ImageIO.write(image, "png", outputStream);
	  outputStream.close();



 }
}
